<?php 

class mysqlClass {

	protected $connection = null;

    #make a connection
	public function __construct($params = array())
	{	
		if(count($params) == 0){
			$params['hostname']=DB_SERVER;
			$params['dbname']=DB_NAME;
			$params['username']=DB_USERNAME;
			$params['password']=DB_PASS;
		}
		try 
		{
            # MySQL with PDO_MYSQL  
			$this->connection = new PDO("mysql:host=".$params['hostname'].";dbname=".$params['dbname'], $params['username'], $params['password']);
			$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
		}
		catch (PDOException $e) 
		{
			$this->connection = null;
			die($e->getMessage());
		}
	}

	#get all the table entries
	public function fetch_result($query, $variables = array())
	{
        # create a prepared statement
		$stmt = $this->connection->prepare($query);

		if($stmt) 
		{
            # execute query 
			$stmt->execute($variables);
			$result= $stmt->fetchAll();
			return $result;
		} 
		else
		{
			return self::get_error();
		}
	}
	
	
	public function fetch_row($table, $value, $column = "id")
	{
		$query = "SELECT * FROM $table WHERE $column = ?";
		
        # create a prepared statement
		$stmt = $this->connection->prepare($query);

		if($stmt) 
		{
            # execute query 
			$stmt->execute(array($value));
			
			$result= $stmt->fetch();
			return $result;
		} 
		else
		{
			return self::get_error();
		}
	}
	
	public function fetch_one($query, $variables = array())
	{
        # create a prepared statement
		$stmt = $this->connection->prepare($query);

		if($stmt) 
		{
            # execute query 
			$stmt->execute($variables);
			$result= $stmt->fetch();
			return $result;
		} 
		else
		{
			return self::get_error();
		}
	}

	#get all the table entries
	public function fetch_table($table_name,$limit = null,$order = null)
	{
		#create query
		$query = "SELECT * FROM {$table_name}";
		if($order != null){
			$query .=" ORDER BY {$order} ";
		}
		if($limit != null){
			$query .=" LIMIT {$limit} "; 
		}

        # create a prepared statement
		$stmt = $this->connection->prepare($query);

		if($stmt) 
		{
            # execute query 
			
			$stmt->execute();
			$result= $stmt->fetchAll();
			return $result;
		} 
		else
		{
			return self::get_error();
		}
	}

	#insert in table
	public function insert($table_name,$params)
	{
		$table_fields=$this->get_colums($table_name);
		#create query				
		foreach ($params as $key => $value) {
			if(in_array($key, $table_fields)){
				$into[]=$key;
				$values[]=$value;
				$query_values[]='?';
			}				
		}
		
		$string_query_values= implode(',',$query_values);
		$into_string = implode(',', $into);
		
		#creating query
		$query_insert= "INSERT INTO {$table_name}({$into_string}) VALUES({$string_query_values})";
		
		
        # create a prepared statement
		$stmt = $this->connection->prepare($query_insert);

		if($stmt) 
		{
			$stmt->execute($values);
			return $this->connection->lastInsertId();			
		} 
		else
		{
			return self::get_error();
		}
	}

	#update table
	public function update($table_name,$params){
		#get colums
		$table_fields=$this->get_colums($table_name);
		
		#make query
		foreach ($params as $key => $value) {
			if(in_array($key, $table_fields) && $key!='id'){
				$update[]=$key."=?";
				$values[]=$value;
			}				
		}
		$values[]=$params['id'];
		$update_string = implode(',', $update);	


		$query_update= "UPDATE {$table_name} SET {$update_string} WHERE id=?";	
        # create a prepared statement
		$stmt = $this->connection->prepare($query_update);

		if($stmt) 
		{
            # execute fetch colums			
			$stmt->execute($values);
		} 
		else
		{
			return self::get_error();
		}
	}

	#delete from table
	public function delete($table_name,$params){

		#create query
		foreach ($params as $key => $value) {
				$delete[]=$key."=?";
				$values[]=$value;
		}
		$delete_string =implode(" AND ", $delete);
		$query = "DELETE FROM {$table_name} WHERE {$delete_string}";

		# create a prepared statement
		$stmt = $this->connection->prepare($query);

		if($stmt) 
		{
            # execute query 
			$stmt->execute($values);

			return $stmt->rowCount();
		} 
		else
		{
			return self::get_error();
		}
	}

    #get the number of rows in a result
	public function num_rows($query, $params = array())
	{
        # create a prepared statement
		$stmt = $this->connection->prepare($query);

		if($stmt) 
		{
            # execute query 
			$stmt->execute($params);

			return $stmt->rowCount();
		} 
		else
		{
			return self::get_error();
		}
	}

	#execute any query (if possible)
	public function execute_query($query,$params=array()){
		 # create a prepared statement
		$stmt = $this->connection->prepare($query);

		if($stmt) 
		{
            # execute query 
			$stmt->execute($params);			
			return true;
		} 
		else
		{
			return self::get_error();
		}
	}

    #display error
	public function get_error() 
	{	
		
		$this->connection->errorInfo();
		
		
	}
	#get table colums
	public function get_colums($table_name){
		$q = $this->connection->prepare("DESCRIBE $table_name");
		$q->execute();	
		$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);

		return $table_fields;
	}

    # closes the database connection when object is destroyed.
	public function __destruct()
	{
		$this->connection = null;
	}


}

?>